package com.ty.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ty.mapper.TeamMapper;
import com.ty.pojo.Team;
import com.ty.pojo.TeamExample;
import com.ty.vo.QueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 球队的业务逻辑层
 * @author 田野
 */
@Service
public class TeamService {
    @Resource
    private TeamMapper teamMapper;

    /**
     * 多条件分页查询
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    public PageInfo<Team> queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){
        // 多条件
        TeamExample teamExample = new TeamExample();
        // 创建多条件查询的容器，动态SQL
        TeamExample.Criteria criteria = teamExample.createCriteria();
        if (vo != null){
            // 三个模糊查询，多条件查询
            if (vo.getTeamName() != null && !"".equals(vo.getTeamName().trim())){
                criteria.andTeamNameLike("%"+ vo.getTeamName().trim() +"%");
            }
            if (vo.getChineseName() != null && !"".equals(vo.getChineseName().trim())){
                criteria.andChineseNameLike("%"+ vo.getChineseName().trim() +"%");
            }
            if (vo.getCoach() != null && !"".equals(vo.getCoach().trim())){
                criteria.andCoachLike("%"+ vo.getCoach().trim() +"%");
            }
            if (vo.getBeginDate() != null){
                criteria.andCreateTimeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate() != null){
                criteria.andCreateTimeLessThanOrEqualTo(vo.getEndDate());
            }

            if (vo.getArea() != null && vo.getArea()>=0){
                criteria.andAreaEqualTo(vo.getArea());
            }


        }

        // 分页 使用插件  注意分页仅对紧邻的SQL语句有效  因为其是利用拦截器的形式实现的
        PageHelper.startPage(pageNum, pageSize);
        List<Team> teams = teamMapper.selectByExample(teamExample);
        return new PageInfo<>(teams);
    }

    /**
     * 添加一个球队
     * 增删改必须要有事务支持,查询可以省略
     * @param team
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addTeam(Team team){
        return teamMapper.insertSelective(team);
    }

    /**
     * 根据主键查询
     * @param teamId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Team queryById(Integer teamId){
        return teamMapper.selectByPrimaryKey(teamId);
    }

    /**
     * 根据主键更新一个球队
     * @param team
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateTeam(Team team){
        return teamMapper.updateByPrimaryKeySelective(team);
    }

    /**
     * 根据主键删除一个球队，逻辑删除
     * @param teamId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteTeam(Integer teamId){
        Team team = teamMapper.selectByPrimaryKey(teamId);
        // 逻辑删除，将要删除的球队的标识置为1
        team.setIsDel(1);
        return teamMapper.updateByPrimaryKeySelective(team);
    }


}
